package com.dev.module_ui_composable.picker

import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Keyboard
import androidx.compose.material.icons.rounded.Schedule
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TimeInput
import androidx.compose.material3.TimePicker
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyTimePicker(
    showTimePicker: MutableState<Boolean>,
    onSelectedTime: (time :String) -> Unit
) {
    val formatter = remember { SimpleDateFormat("HH:mm", Locale.getDefault()) }
    val state = rememberTimePickerState(initialHour = 12, initialMinute = 0)
    val showTimeInput = remember { mutableStateOf(false) }

    if (showTimePicker.value) {
        Dialog(onDismissRequest = { showTimePicker.value = false }) {
            Surface(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(8.dp),
                shape = MaterialTheme.shapes.medium
            ) {
                Column(
                    modifier = Modifier.padding(16.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    val title = if (!showTimeInput.value) "选择时间" else "输入时间"
                    Text(
                        text = title,
                        modifier = Modifier
                            .align(Alignment.Start)
                            .padding(bottom = 8.dp),
                        style = MaterialTheme.typography.titleMedium
                    )

                    if (!showTimeInput.value) {
                        TimePicker(state)
                    } else {
                        TimeInput(state)
                    }

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        val icon = if (!showTimeInput.value) Icons.Rounded.Keyboard else Icons.Rounded.Schedule
                        IconButton(onClick = { showTimeInput.value = !showTimeInput.value }) {
                            Icon(imageVector = icon, contentDescription = "switch")
                        }

                        Row(horizontalArrangement = Arrangement.End) {
                            TextButton(onClick = { showTimePicker.value = false }) {
                                Text("取消")
                            }
                            TextButton(onClick = {
                                val calendar = Calendar.getInstance().apply {
                                    set(Calendar.HOUR_OF_DAY, state.hour)
                                    set(Calendar.MINUTE, state.minute)
                                }
                                onSelectedTime(formatter.format(calendar.time))
                                showTimePicker.value = false
                            }) {
                                Text("确认")
                            }
                        }
                    }
                }
            }
        }
    }
}

@Preview(
    name = "Material TimePicker",
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    locale = "zh-rCN",
    showBackground = true,
    device = "spec:width=1440px,height=3200px,dpi=560"
)
@Composable
private fun TimePickerPreview() {
    val showTimePicker = remember { mutableStateOf(true) }
    MyTimePicker(showTimePicker = showTimePicker) {}
}